//
//  UserAccountViewModel.swift
//  Weibo
//
//  Created by apple on 15/12/13.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import AFNetworking

//封装网络请和 业务逻辑
class UserAccountViewModel: NSObject {
    //定义用户模型对象的属性 
    var userAccount: UserAccount?
    
    
    override init() {
        
        //对象的构造 分 两步
        //先构造本来  再构造父类
//        self.name = name
        userAccount = UserAccount.loadAccount()
        super.init()
    }
    
    //计算性属性 用户是否登陆
    var userLogin: Bool {
        return userAccount?.access_token != nil
    }
    
    //用户名
    
    var userName:String?{
    
        return userAccount?.name
    
    }
    
    //用户头像url
    
    var iconImageURL:NSURL?{
    
        let urlString = userAccount?.avatar_large
        
        return NSURL(string: urlString ?? "")
        
    
    }
    
    
    
    
    
    
    // MARK:加载用户token(请求标识)
    func loadAccessToken(code: String, finished: (isSuccess: Bool) -> ()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":redirect_uri]
        
//        let AFN = AFHTTPSessionManager()
//        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
   NetworkTool.sharedTool.requestDictToNetworkingWith(Method.POST, urlString: urlString, parameters: parameters) { (dict, error) -> () in
    
      if error != nil {
       finished(isSuccess: false)
       return
      }
    
        //字典转模型
        let userAccount = UserAccount(dict: dict!)
        self.loadUserInfo(userAccount, finished: finished)
    
        }
        
//      AFN.POST(urlString, parameters:parameters , success: { (_, result) -> Void in
//        if let dict = result as? [String : AnyObject] {
//            //字典转模型
//            let userAccount = UserAccount(dict: dict)
//            self.loadUserInfo(userAccount, finished: finished)
//        }
//
//        
//      }) { (_, error) -> Void in
//        print(error)
//        finished(isSuccess: false)
//        
//        }
    }
    
    // MARK:获取用户信息
    private func loadUserInfo(account: UserAccount,finished: (isSuccess: Bool) -> ()) {
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 制定中不能够存放nil [NSNull null]
        if let token = account.access_token,userId = account.uid {
            
            print(account.access_token)
            
            
            let parameters = ["access_token": token, "uid": userId]
            let AFN = AFHTTPSessionManager()
            
            
            NetworkTool.sharedTool.requestDictToNetworkingWith(Method.GET, urlString: urlString, parameters: parameters, finished: { (dict, error) -> () in
                
                if error != nil {
                
                    finished(isSuccess: false)
                     return
                }
                
                //获取用户信息
                let avatar_large = dict!["avatar_large"] as! String
                let name = dict!["name"] as! String
                //给account 对象的属性做赋值操作
                account.name = name
                account.avatar_large = avatar_large
                //获取用户信息的完整自定义对象
                //存储自定义对象
                account.saveAccount()
                //执行成功的回调
                finished(isSuccess: true)
                
                
            })
  
//            AFN.GET(urlString, parameters: parameters, success: { (_, result) -> Void in
//                if let dict: AnyObject = result {
//                    //获取用户信息
//                    let avatar_large = dict["avatar_large"] as! String
//                    let name = dict["name"] as! String
//                    //给account 对象的属性做赋值操作
//                    account.name = name
//                    account.avatar_large = avatar_large
//                    //获取用户信息的完整自定义对象
//                    //存储自定义对象
//                    account.saveAccount()
//                    //执行成功的回调
//                    finished(isSuccess: true)
//                }
//            }, failure: { (_, error) -> Void in
//                print(error)
//                finished(isSuccess: false)
//
//            })
//            
            
            
            }
        }
    
  
}


